<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house" style="font-weight: bold">&nbsp;&nbsp;后台管理</i>
        </el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-platform">&nbsp;&nbsp;生产管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-circle-plus">&nbsp;&nbsp;材料管理添加</i></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div>
      <div class="data-canRead" style="width: 100%;background-color: white;height: auto">
        <el-divider content-position="left">数据添加</el-divider>
        <el-form
            :model="materialForm"
            :rules="rules"
            ref="materialForm"
            label-width="100px"
            style="margin-right: 25px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="登记时间:" prop="registrationTime">
                <el-date-picker
                    v-model="materialForm.registrationTime"
                    type="date"
                    format="yyyy 年 MM 月 dd 日"
                    value-format="yyyy-MM-dd"
                    placeholder="选择登记时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="材料名称:" prop="name">
                <el-input v-model="materialForm.name" placeholder="请输入材料名称"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="入库单位:" prop="unitIn">
                <el-input v-model="materialForm.unitIn" placeholder="输入入库单位"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="入库数量:" prop="countIn">
                <el-input v-model="materialForm.countIn" placeholder="输入入库数量"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="入库负责人:" prop="resIn">
                <el-input v-model="materialForm.resOut" placeholder="输入入库负责人"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="出库单位:" prop="unitOut">
                <el-input v-model="materialForm.unitOut" placeholder="输入出库单位"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="出库数量:" prop="countOut">
                <el-input v-model="materialForm.countOut" placeholder="输入出库数量"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="出库负责人:" prop="resOut">
                <el-input v-model="materialForm.resIn" placeholder="输入出库负责人"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="20">
              <el-form-item label="现存数量:" prop="count">
                <el-input v-model="materialForm.count" placeholder="输入现存数量"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item style="float: right">
                <el-button type="primary" @click="compute" style="background-color: white;border-color: gray">
                  <i class="el-icon-thumb" style="color:gray;font-size: 100%">&nbsp;&nbsp;计算</i>
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item>
            <el-button type="primary" @click="submitForm('materialForm')" style="color: white">添加</el-button>
            <el-button type="warning" @click="resetForm('materialForm')" style="color: white">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      materialForm: {
        registrationTime: '2021-01-01',
        name: '五金',
        unitIn: '块',
        countIn: 2500,
        resIn: '阿杰',
        unitOut: '块',
        countOut: 1200,
        resOut: '阿金',
        count: 0,
      },
      rules: {
        registrationTime: [
          {required: true, message: "请选择登记时间", trigger: 'change'}
        ],
        name: [
          {required: true, message: "请输入材料名称", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        unitIn: [
          {required: true, message: "请输入入库单位", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        countIn: [
          {required: true, message: "请输入入库数量", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        resIn: [
          {required: true, message: "请输入入库负责人", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        unitOut: [
          {required: true, message: "请输入出库单位", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        countOut: [
          {required: true, message: "请输入出库数量", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        resOut: [
          {required: true, message: "请输入出库负责人", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        count: [
          {required: true, message: "请输入现存数量", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9080/produceMaterials/add-new';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.materialForm);
          console.log('formData = ' + formData);

          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            console.log(responseBody);
            if (responseBody.state == 20000) {
              this.$message({
                showClose: true,
                message: '添加成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message({
                showClose: true,
                message: responseBody.message,
                type: 'error'
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    compute() {
      let count =
          parseInt(this.materialForm.countIn) -
          parseInt(this.materialForm.countOut);
      this.$set(this.materialForm, 'count', count);
    }
  }
}

</script>

<style scoped>
* {
  color: black;
}

.data-canRead {
  width: 380px;
  height: 380px;
  margin-top: 15px;
  box-shadow: 0px 5px 5px #8d858a;
  float: left;
  margin-right: 16px;
}

</style>